package handlers

import (
	"net/http"

	"api/go-demo/models"
	"api/go-demo/utils"

	"github.com/gin-gonic/gin"
	"github.com/gin-gonic/gin/binding"
	"github.com/jinzhu/gorm"
)

//查询菜单树
func OnFindPerMenuTreeRequest(c *gin.Context) {
	logInfo := c.MustGet("logInfo").(*utils.LogInfo)
	log := logInfo.NewLog("handlers.OnFindPerMenuTreeRequest")
	log.Start("请求查询菜单树开始")
	req := &utils.FindPerMenuTreeReq{}
	if err := c.ShouldBindWith(req, binding.JSON); err != nil {
		responseObj := log.ErrorEnd(utils.ErrBindJSON, "参数绑定异常", err)
		c.JSON(http.StatusOK, responseObj)
		return
	}
	//获取gorm.DB
	db := c.MustGet(utils.DBAUTHORITY).(*gorm.DB)
	tree, err := models.FindPerMenuTree(logInfo, db, req)
	if err != nil {
		resonseObj := log.ErrorEnd(utils.ErrComm, "查询菜单树异常", err)
		c.JSON(http.StatusOK, resonseObj)
		return
	}
	resonseObj := log.SuccessEnd("查询菜单树成功", tree)
	c.JSON(http.StatusOK, resonseObj)
}
